package com.axx.android.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.base.BaseFragment;
import com.axx.android.home.adapter.ViewPagerAdapter;
import com.axx.android.home.fragment.MapFragment;
import com.axx.android.home.fragment.UserListFragment;
import com.axx.android.home.view.NoScrollViewPager;
import com.axx.android.profile.ProfileActivity;
import com.axx.android.subject.SubjectNowActivity;
import com.axx.android.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/21 0021.
 */

public class MainActivity extends BaseActivity {
    @Bind(R.id.tablayout)
    TabLayout mTablayout;
    @Bind(R.id.btn_query)
    Button mBtnQuery;
    @Bind(R.id.btn_top)
    Button mBtnTop;
    @Bind(R.id.viewpager)
    NoScrollViewPager mViewpager;
    @Bind(R.id.btn_user)
    ImageView btnUser;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        initViewPager();
    }

    private void initViewPager() {
        List<BaseFragment> list = new ArrayList<>();

        BaseFragment map = MapFragment.newInstance();
        map.setTitle("首页");

        BaseFragment users = UserListFragment.newInstance();
        users.setTitle("列表");
        list.add(map);
        list.add(users);
        mViewpager.setNoScroll(true);
        mViewpager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), list));
        mTablayout.setupWithViewPager(mViewpager);

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (LetterApp.authUser != null) {
            ImageLoader.loadRoundImage(LetterApp.authUser.getHeadPic(), btnUser, this, R.drawable.ic_default_header_man);
        }
    }

    @OnClick({R.id.btn_user, R.id.btn_query, R.id.btn_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_user:
                startCheckAuth(new Intent(this, ProfileActivity.class));
                break;
            case R.id.btn_query:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case R.id.btn_top:
                startActivity(new Intent(this, SubjectNowActivity.class));
                break;
        }
    }

    //退出时的时间
    private long mExitTime;

    //对返回键进行监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            showToast("再按一次退出");
            mExitTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }
    }
}
